package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;

import br.com.vexillum.model.annotations.TransientField;
import br.com.vexillum.util.HibernateUtils;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.PostGraduationController;
import br.inf.solutions.apresentacoes.ssp.control.RequerimentController;
import br.inf.solutions.apresentacoes.ssp.control.SquareController;
import br.inf.solutions.apresentacoes.ssp.model.PostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.Requeriment;
import br.inf.solutions.apresentacoes.ssp.model.Square;
import br.inf.solutions.apresentacoes.ssp.model.SquarePostGraduation;

@SuppressWarnings("serial")
@org.springframework.stereotype.Component
@Scope("prototype")
public class RequerimentComposer extends
BaseComposer<Requeriment, RequerimentController> {

	@Wire
	private Combobox comboPost;
	@Wire
	private Combobox comboSquare;
	@SuppressWarnings("rawtypes")
	@TransientField
	private List postGraduations;
	@SuppressWarnings("rawtypes")
	@TransientField
	private List square;

	private Square searchSquare;
	
	public Square getSearchSquare() {
		return searchSquare;
	}

	public void setSearchSquare(Square searchSquare) {
		this.searchSquare = searchSquare;
	}
	
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadBinder();
	}

	@Override
	public RequerimentController getControl() {
		return SpringFactory.getController("requerimentController",
				RequerimentController.class,
				ReflectionUtils.prepareDataForPersistence(this));
	}

	//
	@SuppressWarnings("unchecked")
	public Return loadRegistredRequeriment() {
		Requeriment actualRequeriment;
		List<Requeriment> requerimentList = (List<Requeriment>) loadRequeriment();

		if (requerimentList.size() > 0) {
			actualRequeriment = requerimentList.get(0);
		} else {
			actualRequeriment = new Requeriment();
			actualRequeriment.setSquare(entity.getSquare());
			actualRequeriment.setPostGraduation(entity.getPostGraduation());
		}
		setEntity(actualRequeriment);
		loadBinder();
		return new Return(true);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List loadRequeriment() {
		RequerimentController controller = getControl();
		Long squareId = 0l;
		Long postGraduationId = 0l;
		if (entity.getSquare() != null) {
			squareId = entity.getSquare().getId();
		}
		if (entity.getPostGraduation() != null) {
			postGraduationId = entity.getPostGraduation().getId();
		}
		controller.getData().put(
				"sql",
				"From Requeriment where active = true and square.id ="
						+ squareId + " and postGraduation.id="
						+ postGraduationId + " order by id");
		return (ArrayList<PostGraduation>) controller.searchByHQL().getList();
	}

	@Override
	public Requeriment getEntityObject() {
		return new Requeriment();
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	protected String getDeactivationMessage() {
		return "";
	}



	@SuppressWarnings("unchecked")
	public List<Square> getSquares(){
		SquareController controlador = SpringFactory.getController("squareController", SquareController.class, ReflectionUtils.prepareDataForPersistence(this));
		return (List<Square>) controlador.doAction("listAll").getList();
	}
	
	@SuppressWarnings("unchecked")
	public List<PostGraduation> getPosts(){
		if (searchSquare == null) {
			return new ArrayList<PostGraduation>();
		} else {
			getEntity().setSquare(searchSquare);
			List<PostGraduation> list = new ArrayList<>();
			for(SquarePostGraduation sqp : searchSquare.getSquarePostAssociations()){
				PostGraduation post = (PostGraduation) HibernateUtils.initialize(sqp.getPostGraduation());
				list.add(post);
			}
			HashMap<String, Object> data = new HashMap<>();
			data.put("listEntity", list);
			PostGraduationController controller = SpringFactory.getController("postGraduationController", PostGraduationController.class, data);
			
			return (List<PostGraduation>) controller.doAction("sortListByPattern").getList();
		}
		
	}
}